import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class Apple {

    private int price;

    public int getPrice(){
        return price;
    }

    public void setPrice(int price){
        this.price = price;
    }

    public static void main(String[] args) throws Exception {
        Apple apple = new Apple();
        apple.setPrice(5);
        System.out.println("Apple price:"+apple.getPrice());
        //使用反射
        Class clz = Class.forName("Apple");
        Method setMethod = clz.getMethod("setPrice", int.class);
        Constructor appleConstructor = clz.getConstructor();
        Object appleObj = appleConstructor.newInstance();
        setMethod.invoke(appleObj,4);
        Method getPrice = clz.getMethod("getPrice");
        System.out.println("Apple price:"+getPrice.invoke(appleObj));

    }
}
